package me

import grails.converters.JSON
import groovy.sql.Sql
import groovy.json.*
import grails.gorm.*
import org.springframework.messaging.simp.SimpMessageSendingOperations

class MeetingController {
	SimpMessageSendingOperations brokerMessagingTemplate

	def dataSource

    def index() { }

     def store() {

    	def results = []

    	results = Meeting.findAll("""from Meeting order by dateCreated desc""")

      	results = [success:true,total:results.size(),results:results]
		render results  as JSON


  	}


  	def delete() {
  		def meeting = Meeting.findById(params.id)
  		meeting.delete(flush: true) 

  		def files = File.where {
            meetingId == params.id
        }.deleteAll()
		brokerMessagingTemplate.convertAndSend "/topic/notice", '{"flag":1}'
  		def results = [success:true]
    	render results as JSON
  	}

  	def save() {
  		def results = [success:true]
   		

  		def startTime = (new Date()).parse("yyyy-MM-dd HH:mm", params.startDate+" "+params.startTime)


    	if (params.id==null || params.id=="") {

    		def meeting = Meeting.findByCnName(params.cnName)
    		if (meeting==null) {
    			meeting = new Meeting(cnName: params.cnName,startTime:startTime,status:0)
				meeting.save(failOnError: true)
    		} else {
    			results = [success:false,message:'会议名已存在,请重新命名!']
    		}

            results.id = meeting.id
        } else {

    	   def meeting = Meeting.findById(params.id)
    	   meeting.cnName = params.cnName
    	   meeting.startTime = startTime
    	   meeting.save(failOnError: true,flush: true)
    	   results.id = meeting.id
        }

		brokerMessagingTemplate.convertAndSend "/topic/notice", '{"flag":1}'
    	render results as JSON
  	}

	def copy() {
		def results = [success:true]
		def sql = new Sql(dataSource)

		DetachedCriteria<Meeting> query = Meeting.where {
			id != params.id && status == 1

		}

		int total = query.updateAll(status:2)


		def meeting = Meeting.findById(params.id)
		meeting.save(failOnError: true,flush: true)


		def newMeeting = new Meeting(cnName: meeting.cnName,startTime:meeting.startTime,status:1)
		newMeeting.save(failOnError: true)
		results.id = newMeeting.id

		query = File.where {
			meetingId == meeting.id

		}

		def fm = [:]

		query.each{

			def file = new File(cnName:it.cnName,fileUuid:it.fileUuid,meetingId:newMeeting.id,parentFileId:it.parentFileId,
			isDir:it.isDir,fileOrder:it.fileOrder,fileTypeId:it.fileTypeId,password:it.password)
			file.save()
			fm[it.id] = file.id
		}

		query = File.where {
			meetingId == newMeeting.id
		}

		query.each{
			def file = File.findById(it.id)
			file.parentFileId = fm[file.parentFileId]
			file.save(failOnError: true,flush:true)
			file.save()

		}


		brokerMessagingTemplate.convertAndSend "/topic/notice", '{"flag":1}'
		render results as JSON
	}


  	def copy1() {
		def results = [success:true]

		DetachedCriteria<Meeting> query = Meeting.where {
			id != params.id && status == 1

		}


		int total = query.updateAll(status:2)


   		def meeting = Meeting.findById(params.id)
    	meeting.save(failOnError: true,flush: true)


		def newMeeting = new Meeting(cnName: meeting.cnName,startTime:meeting.startTime,status:1)
		newMeeting.save(failOnError: true)
		results.id = newMeeting.id


		query = File.where {
			meetingId == meeting.id

		}

		total = query.updateAll(meetingId:newMeeting.id)
		/*query.each {
			def file = new File(cnName:it.cnName,fileUuid:it.fileUuid,meetingId:newMeeting.id,pasword:it.password,
					parentFileId:it.parentFileId,isDir:it.isDir,fileOrder:it.fileOrder,fileTypeId:it.fileTypeId)
			file.save()
		}*/
		brokerMessagingTemplate.convertAndSend "/topic/notice", '{"flag":1}'
    	render results as JSON
  	}

  	def start() {
		def results = [success:true]



  		DetachedCriteria<Meeting> query = Meeting.where {
			id != params.id && status == 1

		}
		int total = query.updateAll(status:2)

		def meeting = Meeting.findById(params.id)
		meeting.status = 1
		meeting.save(failOnError: true,flush: true)
		results.id = meeting.id
		brokerMessagingTemplate.convertAndSend "/topic/notice", '{"flag":1}'
    	render results as JSON
  	}

  	def stop() {
  		def results = [success:true]
   		
   		def meeting = Meeting.findById(params.id)
    	meeting.status = 2
    	meeting.save(failOnError: true,flush: true)
		results.id = meeting.id
		brokerMessagingTemplate.convertAndSend "/topic/notice", '{"flag":1}'
    	render results as JSON
  	}
}
